package com.langChain4j.conversation.controller;

import com.base.api.entity.RestResponse;
import com.langChain4j.conversation.vo.MessageRequest;
import com.langChain4j.conversation.vo.SessionRequest;
import com.langChain4j.conversation.controller.vo.ConversationSessionVo;
import com.langChain4j.conversation.service.AgentConversationService;
import com.langChain4j.model.Assistant;
import com.langChain4j.model.StreamAssistant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import javax.validation.Valid;

/**
 * 智能体对话交互控制器
 *
 * @author linjun
 */
@RestController
@RequestMapping("/api/v1/conversation")
@Slf4j
@Api(tags = "智能体对话交互接口")
public class AgentConversationController {

    @Autowired
    private AgentConversationService conversationService;

    @Autowired
    private Assistant assistant;

    @Autowired
    private StreamAssistant streamAssistant;

    @PostMapping("/start")
    @ApiOperation("发起对话会话")
    public RestResponse<ConversationSessionVo> startConversation(@RequestBody SessionRequest sessionRequest) {
        return RestResponse.<ConversationSessionVo>builder()
                .data(conversationService.startConversation(sessionRequest))
                .build();
    }

    @PostMapping(value = "/send", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @ApiOperation("发送对话消息")
    public Flux<String> sendMessage(@RequestBody MessageRequest messageRequest) {
        return conversationService.sendMessage(messageRequest);
    }

    @PostMapping(value = "/assistant/chat")
    @ApiOperation("发送对话消息")
    public RestResponse<String> assistantChat(@RequestBody MessageRequest messageRequest) {
        return RestResponse.<String>builder()
                .data(assistant.memoryChat(messageRequest.getSessionId(), messageRequest.getMessageContent()))
                .build();
    }

    @PostMapping(value = "/streamAssistant/chat")
    @ApiOperation("发送对话消息")
    public Flux<String> streamAssistantChat(@RequestBody MessageRequest messageRequest) {
        return streamAssistant.memoryChat(messageRequest.getSessionId(), messageRequest.getMessageContent());
    }

    @PostMapping("/end")
    @ApiOperation("结束对话会话")
    public RestResponse<ConversationSessionVo> endConversation(@Valid @RequestBody SessionRequest sessionRequest) {
        return RestResponse.<ConversationSessionVo>builder()
                .data(conversationService.endConversation(sessionRequest))
                .build();
    }

    @GetMapping("/history")
    @ApiOperation("获取会话历史记录")
    public RestResponse<ConversationSessionVo> getConversationHistory(@RequestParam("sessionId") String sessionId) {
        SessionRequest request = new SessionRequest();
        request.setSessionId(sessionId);
        return RestResponse.<ConversationSessionVo>builder()
                .data(conversationService.getConversationHistory(request))
                .build();
    }
}